<?php

namespace Admin\Controller;

class LayoutController extends AdminController
{
    public function menu()
    {
        $menu = M("node");
        $id = $_POST["id"];
        if ($id == null || "" . equals($id)) {
            //$list=$menu->select();
            $con['level'] = array(
                'elt',
                2,
            );
            $con['ismenu'] = 1;
            $list = $menu->where($con)->select();
            //$list=$list["0"];attributes
            //$list['info'] = 'info';
            //dump($list);
            $list = json_encode($list);
            //$list.put('state','open');
            echo $list; //输出json数据
        } else {
            $list = $menu->where('pid=' + $id)->select();
        }
    }

    // 后台首页 查看系统信息
    public function main()
    {
        $info = array(
            '操作系统' => PHP_OS,
            '运行环境' => $_SERVER["SERVER_SOFTWARE"],
            'PHP运行方式' => php_sapi_name(),
            'PHP版本' => phpversion(),
            'mysql_version' => $this->_mysql_version(),
            'mysql_size' => $this->_mysql_db_size(),
            //'ThinkPHP版本'=>THINK_VERSION.' [ <a href="http://thinkphp.cn" target="_blank">查看最新版本</a> ]',
            '上传附件限制' => ini_get('upload_max_filesize'),
            '执行时间限制' => ini_get('max_execution_time') . '秒',
            '服务器时间' => date("Y年n月j日 H:i:s"),
            '北京时间' => gmdate("Y年n月j日 H:i:s", time() + 8 * 3600),
            '服务器域名/IP' => $_SERVER['SERVER_NAME'] ,
            '剩余空间' => round((@disk_free_space(".") / (1024 * 1024)), 2) . 'M',
            'register_globals' => get_cfg_var("register_globals") == "1" ? "ON" : "OFF",
            'magic_quotes_gpc' => (1 === get_magic_quotes_gpc()) ? 'YES' : 'NO',
            'magic_quotes_runtime' => (1 === get_magic_quotes_runtime()) ? 'YES' : 'NO',
            '系统开发' => "杨庆 email：393978119@qq.com",
        );
        $this->assign('info1', $info);
        // dump(session('user'));
        $this->display();
    }

    private function _mysql_version()
    {
        $Model = M('user');
        $version = $Model->query("select version() as ver");
        return $version[0]['ver'];
    }

    private function _mysql_db_size()
    {
        $Model = M('user');
        $sql = "SHOW TABLE STATUS FROM " . C('DB_NAME');
        $tblPrefix = C('DB_PREFIX');
        if ($tblPrefix != null) {
            $sql .= " LIKE '{$tblPrefix}%'";
        }
        $row = $Model->query($sql);
        $size = 0;
        foreach ($row as $value) {
            $size += $value["data_length"] + $value["index_length"];
        }
        return round(($size / 1048576), 2) . 'M';
    }

    //注册用户提交处理
    public function add()
    {
        $m = D("user");
        if (!$m->create($_POST)) {
            $this->error('创建对象失败' . $m->getError(), 3);
        } else {
            if ($result = $m->add($_POST)) {
                $this->success('添加成功！', 1);
            } else {
                $this->error('添加失败！' . $m->getError(), 0);
            }
        }
    }

    //显示首页公告
    public function gonggao()
    {
        $n = D("Gonggao");
        $list = $n->relation(true)->order("time desc")->select();
        $this->assign('list', $list[0]);
        $this->display();
    }

    public function north()
    {
        $menulist = $this->getMenulist();
        $this->assign("menulist", $menulist);

        $usercon['update_time'] = array("egt", date("Y-m-d H:i:s", time() - (31 * 60)));
        $user = M('User');
        $online = $user->where($usercon)->count();
        $this->assign("online", $online);

        $duanxincount = 0;
        $this->assign("duanxincount", $duanxincount);
        $this->assign("dbname", C('DB_NAME'));
        $Project = A('Project');
        $Projectlist = $Project->viewWidgetIndex();
        $this->assign('projectlist', $Projectlist);
        $this->display();
    }

    public function center()
    {
        $this->display();
    }

    public function portal()
    {
        $this->display();
    }

    public function east()
    {
        $menulist = $this->getMenulist();
        $this->assign("menulist", $menulist);
        $this->display();
    }

    public function west()
    {
        $menulist = $this->getMenulist();
        $this->assign("menulist", $menulist);
        $this->display();
    }

    public function total()
    {
        $this->display();
    }

    private function getMenulist()
    {
        $auth = new \Think\Auth();
        if (session('user.account') == admin) {
            $authlist = M('node')->order('sort asc')->select();
        } else {
            $authlist = $auth->getAuthList(session('user.id'));
        }
        $menulist = array();
        $menulist = $this->erwei($authlist);
        return $menulist;
    }

    //一维数组无限级分类
    public function erwei($cate, $pid = 0)
    {
        $arr = array();
        foreach ($cate as $v) {
            if ($v['pid'] == $pid) {
                if ($v['ismenu'] == 1) {
                    $v['child'] = self::erwei($cate, $v['id']);
                    $arr[] = $v;
                }
            }
        }
        return $arr;
    }

    public function getOnlineUser()
    {
        A('Admin')->update_user_time();
        $usercon['update_time'] = array("egt", date("Y-m-d H:i:s", time() - (31 * 60)));
        $user = M('User');
        $online = $user->where($usercon)->count();

        ///////////////////////回访数据
        $patientids = A('Patient')->getPatientidsOfHuifang();
        $con['status'] = array('neq', '已到');
        $con['userid'] = session('user.id');
        $con['projectid'] = session('projectid');
        if (!empty($patientids)) {
            $con['id'] = array('in', $patientids);
        } else {
            $con['id'] = 0;
        }

        $patient = M('patient')->where($con)->count();
        $res['online'] = $online;
        $res['huifangcount'] = $patient;
        echo (json_encode($res));
    }
}
